package com.delivery.service.impl;

import com.delivery.constant.MessageConstant;
import com.delivery.dto.DishDTO;
import com.delivery.dto.DishPageQueryDTO;
import com.delivery.entity.Dish;
import com.delivery.entity.DishFlavor;
import com.delivery.exception.DeletionNotAllowedException;
import com.delivery.mapper.DishFlavorMapper;
import com.delivery.mapper.DishMapper;
import com.delivery.mapper.SetmealDishMapper;
import com.delivery.result.PageResult;
import com.delivery.service.DishService;
import com.delivery.vo.DishVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    public PageResult paginateDishes(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> dishVOList = dishMapper.paginateDishes(dto);
        return new PageResult(dishVOList.getTotal(), dishVOList.getResult());
    }

    /**
     * 批量删除菜品
     * 需要注意：
     * 删除菜品时，需要判断该菜品是否被套餐关联 setmeal_dish表
     * 起售菜品不能删除
     * 同时删除菜品关联的口味数据 dish_flavor表
     * @param ids 需要删除的菜品id集合
     */
    @Transactional
    @Override
    public void deleteBatchDishes(List<Long> ids) {
        for (Long id :ids){
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            Integer count = setmealDishMapper.countByDishId(id);
            if(count > 0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteBatchByDishId(ids);
    }

    @Override
    public DishVO getDishById(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        //  更新菜品表dish表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);

        // 更新口味表dish_flavor表
        // 这张表需要先删除再插入
        List<Long> ids = new ArrayList<>();
        ids.add(dishDTO.getId());
        dishFlavorMapper.deleteBatchByDishId(ids);

        // 如果flavors不为空，则插入
        // 如果flavors为空，说明想要删除所有口味，则不需要插入
        if (dishDTO.getFlavors() != null && !dishDTO.getFlavors().isEmpty()){
            // 因为前端可能没有返回dishId的字段，所以需要手动设置一下
            dishDTO.getFlavors().forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
            dishFlavorMapper.insertBatch(dishDTO.getFlavors());
        }

    }

    @Override
    public List<Dish> getDishesByCategoryId(Long categoryId) {
        List<Dish> dishes = dishMapper.selectByCategoryId(categoryId);
        return dishes;
    }


    @Override
    public List<DishVO> queryByCategoryId(Integer categoryId) {
        List<DishVO> dishVOList = dishMapper.selectWithFlavorByCategoryId(categoryId);
        return dishVOList;
    }
}
